package net.kts.attraction.subway.adapter;

import java.text.DecimalFormat;
import java.util.List;


import net.kts.attraction.subway.smallpass.model.LastPath;
import net.kts.attraction.subway.smallpass.model.PathResult;
import net.kts.define.AttractionDefine;
import net.kts.main.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SubwayResultAdapter extends ArrayAdapter<PathResult>{

	private Context context = null;
	private List<PathResult> pathResultList = null;
	
	public SubwayResultAdapter(Context context, int textViewResourceId,
			List<PathResult> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		
		this.context = context;
		this.pathResultList = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		PathResult pathResult = this.pathResultList.get(position);
		
		View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.attaction_subway_result_row, null);
        }
        else {
        	return v;
        }
        
//        if(position < 2) {
//        	
//            v = this.settingDefaultData(position);
//
//            return v;
//        }
        
        
//        StationName stationName = this.stationNameList.get(position);
        if (pathResult != null) {
        	
        	String pettern = ".##";
    		DecimalFormat df = new DecimalFormat(pettern);
        	
        		ImageView lineImg = (ImageView)v.findViewById(R.id.subwayResultListImg);
        		TextView stationTextView = (TextView) v.findViewById(R.id.subwayResultListStation);
                TextView textv = (TextView) v.findViewById(R.id.subwayResultListText);
                //TextView stDistance = (TextView) v.findViewById(R.id.curSubwayListDistance);
                if (lineImg != null){
                	lineImg.setImageResource(AttractionDefine.getLineDrawableResourceId(pathResult.getLineNum()));
                }
                if(stationTextView != null) {
                	stationTextView.setText(pathResult.getStationName());
                }                
                if(textv != null){
                	textv.setText("Status : "+ this.getTypeToString(pathResult.getType()) + ", Lead Time : "+ df.format(pathResult.getPathTime()));
                }
        }
        return v;
	}
	
	private View settingDefaultData(int position) {
		View v = null;
		
		LayoutInflater vi = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v = vi.inflate(R.layout.attraction_subway_result_row_top, null);
		LastPath lastPath = this.pathResultList.get(0).getLastPath();
		TextView textView = (TextView)v.findViewById(R.id.subwayResultListText);
        // �ҿ�ð�
        if(position == 0) {
        	textView.setText("Total Lead Time : " + (int)(lastPath.getLastTime() + 1) + " Minute");
        }
        // ��������
        else  {
        	textView.setText("Total Path Station Count : " + lastPath.getPassStationList().size());
        }
        
        		
		return v;
	}
	
	private String getTypeToString(int type) {
		
		if(type == 1) {
			return "Start";
		}
		else if(type == 2) {
			return "Arrive";
		}
		else if(type == 3) {
			return "Progress";
		}
		else {
			return "Transfer";
		}
	}

	
	
}
